package com.sinvo.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sinvo.oa.entity.model.process.Process;
import com.sinvo.oa.entity.model.system.SysDept;
import com.sinvo.oa.entity.model.system.SysPost;
import com.sinvo.oa.entity.model.system.SysUser;
import com.sinvo.oa.mapper.ProcessMapper;
import com.sinvo.oa.result.R;
import com.sinvo.oa.service.SysDeptService;
import com.sinvo.oa.service.SysPostService;
import com.sinvo.oa.service.SysUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 首页
 */
@Api(tags = "获取统计数据接口")
@RestController
@RequestMapping("admin/system/dashboard")
public class DashboardController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysPostService sysPostService;

    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private ProcessMapper processMapper;

    /**
     * 数据统计
     *
     * @return
     */
    @GetMapping("getSummary")
    public R getSummary() {
        Map<String, Object> countMap = new HashMap<>();
        //用户数量
        LambdaQueryWrapper<SysUser> userWrapper = new LambdaQueryWrapper();
        userWrapper.eq(SysUser::getStatus, 1).eq(SysUser::getIsDeleted, 0);
        int userCount = sysUserService.count(userWrapper);
        //部门数量
        LambdaQueryWrapper<SysPost> postWrapper = new LambdaQueryWrapper<>();
        postWrapper.eq(SysPost::getStatus, 1).eq(SysPost::getIsDeleted, 0);
        int postCount = sysPostService.count(postWrapper);
        //岗位数量
        LambdaQueryWrapper<SysDept> deptWrapper = new LambdaQueryWrapper<>();
        deptWrapper.eq(SysDept::getStatus, 1).eq(SysDept::getIsDeleted, 0);
        int deptCount = sysDeptService.count(deptWrapper);
        countMap.put("userCount", userCount);
        countMap.put("postCount", postCount);
        countMap.put("deptCount", deptCount);
        return R.success(countMap);
    }

    @GetMapping("getProcessCount")
    public R getProcessCount() {
        //储存结果
        List<Integer> countList = new ArrayList<>();
        List<String> dateList = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            //相差天数分别为 1 2 3...的日期的审批数
            Integer count = processMapper.queryCountByDateDiff(i);
            countList.add(count);
            //相差天数分别为 1 2 3...的日期
            String dateStr = LocalDate.now().minusDays(i).toString();
            dateList.add(dateStr);
        }
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("countList", countList);
        resMap.put("dateList", dateList);
        return R.success(resMap);
    }
}
